﻿using System;
using System.Collections.Generic;
using System.Text;
using Slf.Factories;
using Slf.Resolvers;

namespace Slf.Samples.HierachicalLoggerNames
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.ForegroundColor = ConsoleColor.Green;
      Console.Out.WriteLine("The NamedFactoryResolver class provides hiearchical lookups of logger factories out of the box.\n\n");
      Console.ResetColor();

      //use a named factory resolver, which provides hierarchical lookups out of the box
      NamedFactoryResolver resolver = new NamedFactoryResolver();
 
      //register two named logger factories
      resolver.RegisterFactory("foo.bar", new DebugLoggerFactory());
      resolver.RegisterFactory("foo", new TraceLoggerFactory());

      //log all output for other names to console
      resolver.RegisterFactory(LoggerService.DefaultLoggerName, new ConsoleLoggerFactory());

      //plug in the new resolver
      LoggerService.FactoryResolver = resolver;
      
      CreateLogEntries();
      Console.ReadLine();
    }


    private static void CreateLogEntries()
    {
      //the factory will always return the most specific match possible, with a fallback
      //to the default logger (and if no default logger was available, a NullLogger would be returned)

      WriteInfoToConsole("foo");
      WriteInfoToConsole("foo.bar");
      WriteInfoToConsole("foo.TEST");
      WriteInfoToConsole("foo.bar.com");
      WriteInfoToConsole("Test");
      WriteInfoToConsole(LoggerService.DefaultLoggerName);
    }

    private static void WriteInfoToConsole(string loggerName)
    {
      ILogger logger = LoggerService.GetLogger(loggerName);
      Console.Out.WriteLine("Requesting logger '{0}' returns logger '{1}' of type '{2}'", loggerName,
                            logger.Name, logger.GetType().Name);
    }
  }

}